const PENDING = 'pending'; // 等待
const FULFILLED = 'fulfilled' // 成功
const REJECTED = 'rejected' // 失败

class MyPromise {
    constructor(executor) {
        try {
            executor(this.resolve, this.reject)
        } catch (error) {
            this.reject(error)
        }
    }
    // promise 状态
    status = PENDING
    // 成功之后的值
    value = undefined
    // 失败后的原因
    reason = undefined
    // 成功回调
    successCallback = []
    // successCallback = undefined
    // 失败回调
    failCallback = []
    // failCallback = undefined

    change = (status, value) => {
        // 如果状态不是等待 阻止程序向下执行
        if (this.status !== PENDING) return
        this.status = status
        this.value = value

        // 如果是成功回调
        if (status === FULFILLED) {
            while (this.successCallback.length) this.successCallback.shift()()
        } else {
            while (this.failCallback.length) this.failCallback.shift()()
        }
    }

    resolve = value => {
        this.change(FULFILLED, value)
        // // 如果状态不是等待 阻止程序向下执行
        // if (this.status !== PENDING) return
        // // 将状态更改为成功
        // this.status = FULFILLED
        // // 保存成功之后的值
        // this.value = value
        // // 判断成功回调是否存在 如果存在 调用
        // // this.successCallback && this.successCallback(this.value)
        // while (this.successCallback.length) {
        //     this.successCallback.shift()()
        // }
    }
    reject = reason => {
        this.change(REJECTED, reason)
        // // 如果状态不是等待 阻止程序向下执行
        // if (this.status !== PENDING) return
        // // 将状态更改为失败
        // this.status = REJECTED
        // // 保存失败后的原因
        // this.reason = reason
        // // 判断失败回调是否存在 如果存在 调用
        // // this.failCallback && this.failCallback(this.reason)
        // while (this.failCallback.length) {
        //     this.failCallback.shift()()
        // }
    }
    then(successCallback, failCallback) {
        successCallback = successCallback ? successCallback : value => value
        failCallback = failCallback ? failCallback : reason => {
            throw reason
        }
        let promise2 = new MyPromise((resolve, reject) => {
            // 判断状态
            if (this.status === FULFILLED) {
                // 增加setTimeout目的是为了将下面代码变成异步代码以获取得到promise2对象
                setTimeout(() => {
                    try {
                        let x = successCallback(this.value)
                        // console.log(x, 'test::::')
                        // 判断 x 的值是普通值还是promise对象
                        // 如果是普通值 直接调用resolve
                        // 如果是promise对象 查看promise对象返回的结果
                        // 再根据promised对象返回的结果 决定调用resolve 还是调用reject
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                }, 0)
            } else if (this.status === REJECTED) {
                // 增加setTimeout目的是为了将下面代码变成异步代码以获取得到promise2对象
                setTimeout(() => {
                    try {
                        let x = failCallback(this.reason)
                        // console.log(x, 'test::::')
                        // 判断 x 的值是普通值还是promise对象
                        // 如果是普通值 直接调用resolve
                        // 如果是promise对象 查看promise对象返回的结果
                        // 再根据promised对象返回的结果 决定调用resolve 还是调用reject
                        resolvePromise(promise2, x, resolve, reject)
                    } catch (error) {
                        reject(error)
                    }
                }, 0)
            } else {
                // 等待
                // 将成功回调和失败回调存储起来
                this.successCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let x = successCallback(this.value)
                            // console.log(x, 'test::::')
                            // 判断 x 的值是普通值还是promise对象
                            // 如果是普通值 直接调用resolve
                            // 如果是promise对象 查看promise对象返回的结果
                            // 再根据promised对象返回的结果 决定调用resolve 还是调用reject
                            resolvePromise(promise2, x, resolve, reject)
                        } catch (error) {
                            reject(error)
                        }
                    }, 0)
                });
                this.failCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let x = failCallback(this.reason)
                            // console.log(x, 'test::::')
                            // 判断 x 的值是普通值还是promise对象
                            // 如果是普通值 直接调用resolve
                            // 如果是promise对象 查看promise对象返回的结果
                            // 再根据promised对象返回的结果 决定调用resolve 还是调用reject
                            resolvePromise(promise2, x, resolve, reject)
                        } catch (error) {
                            reject(error)
                        }
                    }, 0)
                })
                // this.successCallback = successCallback;
                // this.failCallback = failCallback
            }
        });

        return promise2
    }
    finally(callback) {
        return this.then(value => {
            return MyPromise.resolve(callback()).then(() => value)
            // callback()
            // return value
        }, reason => {
            return MyPromise.resolve(callback()).then(() => {
                throw reason
            })
            // callback()
            // throw reason
        })
    }
    // catch
    catch (failCallback) {
        return this.then(undefined, failCallback)
    }
    static all(array) {
        let result = []
        let index = 0
        return new MyPromise((resolve, reject) => {
            function addData(key, value) {
                result[key] = value
                index++
                if (index === array.length) {
                    resolve(result)
                }
            }
            for (let i = 0, len = array.length; i < len; ++i) {
                let current = array[i]
                if (current instanceof MyPromise) {
                    // promise对象
                    current.then(value => addData(i, value), reason => reject(reason))
                } else {
                    // 普通值
                    addData(i, array[i])
                }
            }
        })
    }
    static resolve(value) {
        if (value instanceof MyPromise) return value
        return new MyPromise(resolve => resolve(value))
    }
    static reject(reason) {
        if (value instanceof MyPromise) return value
        return new MyPromise((_, reject) => reject(reason))
    }
}

function resolvePromise(myPromise, x, resolve, reject) {
    if (myPromise === x) {
        reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
    }
    if (x instanceof MyPromise) {
        // promise 对象
        x.then(value => resolve(value), reason => reject(reason))
        // console.log(resolve, 'resolve::::')
        //     x.then(resolve, reject)
    } else {
        // 普通值
        resolve(x)
    }
}

module.exports = MyPromise